package com.qhgrain.app.service;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.paas.base.modules.utils.lang.StringUtil;
import com.paas.base.modules.utils.page.PageBean;
import com.qhgrain.app.constant.UrlEnum;
import com.qhgrain.app.controller.UserAppsController;
import com.qhgrain.app.util.ConfigUtil;
import com.qhgrain.app.vo.UserAppsVo;
import com.qhgrain.gway.util.HttpUtil;

@Service
public class UserAppsService {

private static Logger LOG = LoggerFactory.getLogger(UserAppsController.class);
	
	static String serverUrl = ConfigUtil.getProperties(UrlEnum.APP)+"/application";
	
	public PageBean<UserAppsVo> queryList(String userName, String roleName, String appName, String pageNo, String pageSize){
		Map<String,Object> map = new HashMap<String,Object>();
		if(StringUtil.isNotBlank(userName)){
			map.put("userName", userName.trim());
		}
		if(StringUtil.isNotBlank(roleName)){
			map.put("roleName", roleName.trim());
		}
		if(StringUtil.isNotBlank(appName)){
			map.put("appName", appName.trim());
		}
		map.put("pageNo", pageNo);
		map.put("pageSize", pageSize);
		
		String requestVo;
		PageBean<UserAppsVo> pageBean = new PageBean<UserAppsVo>(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
		try {
			requestVo = HttpUtil.postFormParams(serverUrl,"/queryUserApps", map);
			JSONObject voJson = JSONObject.parseObject(requestVo);
			
			if("1".equals(voJson.getString("state"))){
				
				JSONObject dataJson = JSONObject.parseObject(voJson.getString("data"));
				pageBean.setData(JSONArray.parseArray(dataJson.getString("list"),UserAppsVo.class));
				pageBean.setTotalCount(dataJson.getIntValue("totalCount"));
			}else{
				LOG.error("用户群组应用分页查询，api失败。。。"+voJson);
			}
		}catch (Exception e) {
			LOG.error("用户群组应用分页查询查询出错。。。",e);
		}
		return pageBean;
	}
}
